
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>8.3. 如何(不)使用装饰器 · MobX 中文文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="trace.html" />
    
    
    <link rel="prev" href="pitfalls.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1. MobX 介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../intro/overview.html">
            
                <a href="../intro/overview.html">
            
                    
                    1.1. MobX 要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../intro/concepts.html">
            
                <a href="../intro/concepts.html">
            
                    
                    1.2. 概念与原则
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    2. API概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3. observable 相关
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../refguide/observable.html">
            
                <a href="../refguide/observable.html">
            
                    
                    3.1. observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../refguide/observable-decorator.html">
            
                <a href="../refguide/observable-decorator.html">
            
                    
                    3.2. @observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../refguide/object.html">
            
                <a href="../refguide/object.html">
            
                    
                    3.3. objects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../refguide/array.html">
            
                <a href="../refguide/array.html">
            
                    
                    3.4. arrays
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../refguide/map.html">
            
                <a href="../refguide/map.html">
            
                    
                    3.5. maps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../refguide/boxed.html">
            
                <a href="../refguide/boxed.html">
            
                    
                    3.6. boxed values
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../refguide/modifiers.html">
            
                <a href="../refguide/modifiers.html">
            
                    
                    3.7 decorators
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4. 对 observables 作出响应
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../refguide/computed-decorator.html">
            
                <a href="../refguide/computed-decorator.html">
            
                    
                    4.1. (@)computed
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../refguide/autorun.html">
            
                <a href="../refguide/autorun.html">
            
                    
                    4.2. autorun
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../refguide/when.html">
            
                <a href="../refguide/when.html">
            
                    
                    4.3. when
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../refguide/reaction.html">
            
                <a href="../refguide/reaction.html">
            
                    
                    4.4. reaction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../refguide/observer-component.html">
            
                <a href="../refguide/observer-component.html">
            
                    
                    4.5. (@)observer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="react.html">
            
                <a href="react.html">
            
                    
                    4.6. 理解 MobX 对何作出响应
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5. 改变 observables
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../refguide/action.html">
            
                <a href="../refguide/action.html">
            
                    
                    5.1. action
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    5.2. async actions & flows
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../refguide/object-api.html">
            
                <a href="../refguide/object-api.html">
            
                    
                    5.3. Object api
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6. 工具函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../refguide/tojson.html">
            
                <a href="../refguide/tojson.html">
            
                    
                    6.1. toJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../refguide/extend-observable.html">
            
                <a href="../refguide/extend-observable.html">
            
                    
                    6.2. extendObservable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../refguide/extending.html">
            
                <a href="../refguide/extending.html">
            
                    
                    6.3. createAtom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../refguide/observe.html">
            
                <a href="../refguide/observe.html">
            
                    
                    6.4. intercept & observe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
            
                    
                    6.5. mobxUtils.fromPromise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
            
                    
                    6.6. mobxUtils.fromResource
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
            
                    
                    6.7. mobxUtils.toStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
            
                    
                    6.8. mobxUtils.fromStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
            
                    
                    6.9. mobxUtils.now
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../refguide/create-transformer.html">
            
                <a href="../refguide/create-transformer.html">
            
                    
                    6.10. mobxUtils.createTransformer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../refguide/expr.html">
            
                <a href="../refguide/expr.html">
            
                    
                    6.11. mobxUtils.expr
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    6.12. complete api overview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
            
                    
                    7. 博客、 视频及相关项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    8. 贴士与技巧
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../faq/faq.html">
            
                <a href="../faq/faq.html">
            
                    
                    8.1. 常见问题解答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="pitfalls.html">
            
                <a href="pitfalls.html">
            
                    
                    8.2. 常见陷阱与最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.8.3" data-path="decorators.html">
            
                <a href="decorators.html">
            
                    
                    8.3. 如何(不)使用装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="trace.html">
            
                <a href="trace.html">
            
                    
                    8.4. 使用 trace 进行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="store.html">
            
                <a href="store.html">
            
                    
                    8.5. 定义数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="react-performance.html">
            
                <a href="react-performance.html">
            
                    
                    8.6. 优化 React 组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="devtools.html">
            
                <a href="devtools.html">
            
                    
                    8.7. 开发工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../refguide/spy.html">
            
                <a href="../refguide/spy.html">
            
                    
                    8.8. spy
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../donating.html">
            
                <a href="../donating.html">
            
                    
                    9. 捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >8.3. 如何(不)使用装饰器</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5982;&#x4F55;&#x4E0D;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;"><a name="&#x5982;&#x4F55;&#x4E0D;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;" class="plugin-anchor" href="#&#x5982;&#x4F55;&#x4E0D;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5982;&#x4F55;(&#x4E0D;)&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;</h1>
<p>&#x5728;MobX &#x4E2D;&#x4F7F;&#x7528; ES.next &#x88C5;&#x9970;&#x5668;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;&#x672C;&#x7AE0;&#x8282;&#x5C06;&#x89E3;&#x91CA;&#x5982;&#x4F55;(&#x907F;&#x514D;)&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x4F18;&#x52BF;:</p>
<ul>
<li>&#x6837;&#x677F;&#x6587;&#x4EF6;&#x6700;&#x5C0F;&#x5316;&#xFF0C;&#x58F0;&#x660E;&#x5F0F;&#x4EE3;&#x7801;&#x3002;</li>
<li>&#x6613;&#x4E8E;&#x4F7F;&#x7528;&#x548C;&#x9605;&#x8BFB;&#x3002;&#x5927;&#x591A;&#x6570; MobX &#x7528;&#x6237;&#x90FD;&#x5728;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x52A3;&#x52BF;:</p>
<ul>
<li>ES.next 2&#x9636;&#x6BB5;&#x7279;&#x6027;&#x3002;</li>
<li>&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x548C;&#x7F16;&#x8BD1;&#xFF0C;&#x76EE;&#x524D;&#x53EA;&#x6709; Babel/Typescript &#x7F16;&#x8BD1;&#x5668;&#x652F;&#x6301;&#x3002;</li>
</ul>
<p>&#x5728; MobX &#x4E2D;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x3002;</p>
<ol>
<li>&#x5F00;&#x542F;&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x5B9E;&#x9A8C;&#x6027;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5; (&#x8BE6;&#x7EC6;&#x8BF7;&#x53C2;&#x89C1;&#x4E0B;&#x9762;)</li>
<li>&#x4E0D;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#xFF0C;&#x800C;&#x662F;&#x5229;&#x7528; MobX &#x5185;&#x7F6E;&#x7684;&#x5DE5;&#x5177; <code>decorate</code> &#x6765;&#x5BF9;&#x7C7B;&#x548C;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x88C5;&#x9970;&#x3002;</li>
</ol>
<p>&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { observable, computed, action } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx&quot;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> </span>{
    @observable start = <span class="hljs-built_in">Date</span>.now();
  @observable current = <span class="hljs-built_in">Date</span>.now();

  @computed
  get elapsedTime() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.current - <span class="hljs-keyword">this</span>.start + <span class="hljs-string">&quot;milliseconds&quot;</span>;
  }

  @action
  tick() {
    <span class="hljs-keyword">this</span>.current = <span class="hljs-built_in">Date</span>.now();
  }
}
</code></pre>
<p>&#x4F7F;&#x7528; <code>decorate</code> &#x5DE5;&#x5177;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { observable, computed, action, decorate } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx&quot;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> </span>{
    start = <span class="hljs-built_in">Date</span>.now();
    current = <span class="hljs-built_in">Date</span>.now();

    get elapsedTime() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.current - <span class="hljs-keyword">this</span>.start + <span class="hljs-string">&quot;milliseconds&quot;</span>;
  }

    tick() {
        <span class="hljs-keyword">this</span>.current = <span class="hljs-built_in">Date</span>.now()
    }
}
decorate(Timer, {
    start: observable,
    current: observable,
    elapsedTime: computed,
    tick: action
})
</code></pre>
<p>&#x60F3;&#x8981;&#x5728;&#x5355;&#x4E2A;&#x5C5E;&#x6027;&#x4E0A;&#x5E94;&#x7528;&#x591A;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x7684;&#x8BDD;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x6570;&#x7EC4;&#x3002;&#x591A;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x7684;&#x987A;&#x5E8F;&#x662F;&#x4ECE;&#x4ECE;&#x53F3;&#x81F3;&#x5DE6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { decorate, observable } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx&quot;</span>
<span class="hljs-keyword">import</span> { serializable, primitive } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;serializr&quot;</span>
<span class="hljs-keyword">import</span> persist <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx-persist&quot;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Todo</span> </span>{
    id = <span class="hljs-built_in">Math</span>.random();
    title = <span class="hljs-string">&quot;&quot;</span>;
    finished = <span class="hljs-literal">false</span>;
}
decorate(Todo, {
    title: [serializable(primitive), persist(<span class="hljs-string">&quot;object&quot;</span>), observable],
    finished: [serializable(primitive), observable]
})
</code></pre>
<p>&#x6CE8;&#x610F;: &#x5E76;&#x975E;&#x6240;&#x6709;&#x7684;&#x88C5;&#x9970;&#x5668;&#x90FD;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x8D77;&#x7EC4;&#x5408;&#xFF0C;&#x6B64;&#x529F;&#x80FD;&#x53EA;&#x4F1A;&#x5C3D;&#x529B;&#x800C;&#x4E3A;&#x3002;&#x4E00;&#x4E9B;&#x88C5;&#x9970;&#x5668;&#x4F1A;&#x76F4;&#x63A5;&#x5F71;&#x54CD;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x201C;&#x9690;&#x85CF;&#x201D;&#x5176;&#x4ED6;&#x90A3;&#x4E9B;&#x53EA;&#x66F4;&#x6539;&#x539F;&#x578B;&#x7684;&#x88C5;&#x9970;&#x5668;&#x7684;&#x6548;&#x679C;&#x3002;</p>
<hr>
<p><code>mobx-react</code> &#x4E2D;&#x7684; <code>observer</code> &#x51FD;&#x6570;&#x65E2;&#x662F;&#x88C5;&#x9970;&#x5668;&#x53C8;&#x662F;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4E0B;&#x9762;&#x8FD9;&#x4E9B;&#x8BED;&#x6CD5;&#x90FD;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x8FD0;&#x884C;:</p>
<pre><code class="lang-javascript">@observer
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    <span class="hljs-comment">/* ... */</span>
}

<span class="hljs-keyword">const</span> Timer = observer(<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    <span class="hljs-comment">/* ... */</span>
})

<span class="hljs-keyword">const</span> Timer = observer((props) =&gt; (
    <span class="hljs-comment">/* &#x6E32;&#x67D3; */</span>
))
</code></pre>
<h2 id="&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;"><a name="&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;" class="plugin-anchor" href="#&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;</h2>
<p>&#x5982;&#x679C;&#x60F3;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x9700;&#x8981;&#x6309;&#x7167;&#x4E0B;&#x5217;&#x6B65;&#x9AA4;&#x3002;</p>
<p><strong>TypeScript</strong></p>
<p>&#x5728; <code>tsconfig.json</code> &#x4E2D;&#x542F;&#x7528;&#x7F16;&#x8BD1;&#x5668;&#x9009;&#x9879; <code>&quot;experimentalDecorators&quot;: true</code> &#x3002;</p>
<p><strong>Babel: &#x4F7F;&#x7528; <code>babel-preset-mobx</code></strong></p>
<p>&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x5728; Babel &#x4E2D;&#x914D;&#x7F6E; MobX &#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528; <a href="https://github.com/zwhitchcox/babel-preset-mobx" target="_blank"><code>mobx</code></a> preset&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x66F4;&#x65B9;&#x4FBF;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x4E86;&#x88C5;&#x9970;&#x5668;&#x53CA;&#x5176;&#x4ED6;&#x51E0;&#x4E2A;&#x7ECF;&#x5E38;&#x4E0E; mobx &#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x7684;&#x63D2;&#x4EF6;:</p>
<pre><code>npm install --save-dev babel-preset-mobx
</code></pre><p>.babelrc:</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;presets&quot;</span>: [<span class="hljs-string">&quot;mobx&quot;</span>]
}
</code></pre>
<p><strong>Babel: &#x624B;&#x52A8;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;</strong></p>
<p>&#x8981;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;&#x7684;&#x652F;&#x6301;&#x800C;&#x4E0D;&#x4F7F;&#x7528; mobx preset &#x7684;&#x8BDD;&#xFF0C;&#x9700;&#x8981;&#x6309;&#x7167;&#x4E0B;&#x5217;&#x6B65;&#x9AA4;&#x3002;
&#x5B89;&#x88C5;&#x652F;&#x6301;&#x88C5;&#x9970;&#x5668;&#x6240;&#x9700;&#x4F9D;&#x8D56;: <code>npm i --save-dev babel-plugin-transform-decorators-legacy</code> &#x3002;
&#x5E76;&#x5728; <code>.babelrc</code> &#x6587;&#x4EF6;&#x4E2D;&#x542F;&#x7528;:</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;presets&quot;</span>: [<span class="hljs-string">&quot;es2015&quot;</span>, <span class="hljs-string">&quot;stage-1&quot;</span>],
  <span class="hljs-string">&quot;plugins&quot;</span>: [<span class="hljs-string">&quot;transform-decorators-legacy&quot;</span>]
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x63D2;&#x4EF6;&#x7684;&#x987A;&#x5E8F;&#x5F88;&#x91CD;&#x8981;: <code>transform-decorators-legacy</code> &#x5E94;&#x8BE5;&#x653E;&#x5728;<strong>&#x9996;&#x4F4D;</strong>&#x3002;
babel &#x8BBE;&#x7F6E;&#x6709;&#x95EE;&#x9898;&#xFF1F;&#x8BF7;&#x5148;&#x53C2;&#x8003;&#x8FD9;&#x4E2A; <a href="https://github.com/mobxjs/mobx/issues/105" target="_blank">issue</a> &#x3002;</p>
<p>&#x5BF9;&#x4E8E; babel 7, &#x53C2;&#x89C1; <a href="https://github.com/mobxjs/mobx/issues/1352" target="_blank">issue 1352</a> &#x6765;&#x67E5;&#x770B;&#x8BBE;&#x7F6E;&#x793A;&#x4F8B;&#x3002;</p>
<h2 id="&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;-&#x548C;-create-react-app"><a name="&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;-&#x548C;-create-react-app" class="plugin-anchor" href="#&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;-&#x548C;-create-react-app"><i class="fa fa-link" aria-hidden="true"></i></a>&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5; &#x548C; Create React App</h2>
<ul>
<li><code>create-react-app</code> &#x76EE;&#x524D;&#x8FD8;&#x6CA1;&#x6709;&#x5185;&#x7F6E;&#x7684;&#x88C5;&#x9970;&#x5668;&#x652F;&#x6301;&#x3002;&#x8981;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; eject &#x547D;&#x4EE4; &#x6216;&#x4F7F;&#x7528; <a href="https://github.com/timarney/react-app-rewired/tree/master/packages/react-app-rewire-mobx" target="_blank">react-app-rewired</a>&#x3002;</li>
</ul>
<hr>
<h2 id="&#x514D;&#x8D23;&#x58F0;&#x660E;-&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x5C40;&#x9650;&#x6027;"><a name="&#x514D;&#x8D23;&#x58F0;&#x660E;-&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x5C40;&#x9650;&#x6027;" class="plugin-anchor" href="#&#x514D;&#x8D23;&#x58F0;&#x660E;-&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x5C40;&#x9650;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x514D;&#x8D23;&#x58F0;&#x660E;: &#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x7684;&#x5C40;&#x9650;&#x6027;:</h2>
<p><em>&#x5F53;&#x524D;&#x7F16;&#x8BD1;&#x5668;&#x6240;&#x5B9E;&#x73B0;&#x7684;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x662F;&#x6709;&#x4E00;&#x4E9B;&#x9650;&#x5236;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x4E0E;&#x5B9E;&#x9645;&#x7684;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x8868;&#x73B0;&#x5E76;&#x975E;&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x3002;
&#x6B64;&#x5916;&#xFF0C;&#x5728;&#x6240;&#x6709;&#x7F16;&#x8BD1;&#x5668;&#x90FD;&#x5B9E;&#x73B0;&#x7B2C;&#x4E8C;&#x9636;&#x6BB5;&#x7684;&#x63D0;&#x8BAE;&#x4E4B;&#x524D;&#xFF0C;&#x8BB8;&#x591A;&#x7EC4;&#x5408;&#x6A21;&#x5F0F;&#x76EE;&#x524D;&#x90FD;&#x65E0;&#x6CD5;&#x4E0E;&#x88C5;&#x9970;&#x5668;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x3002;
&#x7531;&#x4E8E;&#x8FD9;&#x4E2A;&#x539F;&#x56E0;&#xFF0C;&#x76EE;&#x524D;&#x5728; MobX &#x4E2D;&#x5BF9;&#x88C5;&#x9970;&#x5668;&#x8BED;&#x6CD5;&#x652F;&#x6301;&#x7684;&#x8303;&#x56F4;&#x8FDB;&#x884C;&#x4E86;&#x9650;&#x5B9A;&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x652F;&#x6301;&#x7684;&#x7279;&#x6027;&#x5728;&#x6240;&#x6709;&#x73AF;&#x5883;&#x4E2D;&#x59CB;&#x7EC8;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#x3002;</em></p>
<p>MobX &#x793E;&#x533A;&#x5E76;&#x6CA1;&#x6709;&#x6B63;&#x5F0F;&#x652F;&#x6301;&#x4EE5;&#x4E0B;&#x6A21;&#x5F0F;:</p>
<ul>
<li>&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x7EE7;&#x627F;&#x6811;&#x4E2D;&#x7684;&#x88C5;&#x9970;&#x7C7B;&#x6210;&#x5458;</li>
<li>&#x88C5;&#x9970;&#x9759;&#x6001;&#x7C7B;&#x6210;&#x5458;</li>
<li>&#x5C06; MobX &#x63D0;&#x4F9B;&#x7684;&#x88C5;&#x9970;&#x5668;&#x4E0E;&#x5176;&#x4ED6;&#x88C5;&#x9970;&#x5668;&#x7EC4;&#x5408;</li>
<li>&#x70ED;&#x66F4;&#x65B0; (HMR) / React-hot-loader &#x53EF;&#x80FD;&#x4E0D;&#x80FD;&#x6B63;&#x5E38;&#x8FD0;&#x884C;</li>
</ul>
<p>&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x8BFB;/&#x5199;&#x5230;&#x88C5;&#x9970;&#x5C5E;&#x6027;&#x4E4B;&#x524D;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x5728;&#x7C7B;&#x5B9E;&#x4F8B;&#x4E0A;&#x53EF;&#x80FD;&#x662F;&#x4E0D;&#x53EF;&#x89C1;&#x7684;&#x3002;</p>
<p>(&#x6CE8;&#x610F;: &#x4E0D;&#x652F;&#x6301;&#x5E76;&#x4E0D;&#x610F;&#x5473;&#x7740;&#x4E0D;&#x80FD;&#x8FD0;&#x884C;&#xFF0C;&#x5B83;&#x7684;&#x610F;&#x4E49;&#x5728;&#x4E8E;&#x5982;&#x679C;&#x4E0D;&#x80FD;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#x7684;&#x8BDD;&#xFF0C;&#x5728;&#x5B98;&#x65B9;&#x89C4;&#x8303;&#x7684;&#x63A8;&#x8FDB;&#x4E4B;&#x524D;&#xFF0C;&#x63D0;&#x51FA;&#x7684; issues &#x662F;&#x4E0D;&#x4F1A;&#x88AB;&#x5904;&#x7406;&#x7684;&#x3002;)</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="pitfalls.html" class="navigation navigation-prev " aria-label="Previous page: 8.2. 常见陷阱与最佳实践">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="trace.html" class="navigation navigation-next " aria-label="Next page: 8.4. 使用 trace 进行调试">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"8.3. 如何(不)使用装饰器","level":"1.8.3","depth":2,"next":{"title":"8.4. 使用 trace 进行调试","level":"1.8.4","depth":2,"path":"best/trace.md","ref":"best/trace.md","articles":[]},"previous":{"title":"8.2. 常见陷阱与最佳实践","level":"1.8.2","depth":2,"path":"best/pitfalls.md","ref":"best/pitfalls.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","edit-link","anchors","copy-code-button","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/SangKa/mobx-docs-cn"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"完善文档","base":"https://github.com/SangKa/mobx-docs-cn/tree/master/docs"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MobX 中文文档","gitbook":"3.2.2"},"file":{"path":"best/decorators.md","mtime":"2018-09-18T13:06:39.931Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-18T13:06:52.064Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

